﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Activation;

using NiggleAmp.DAP;
using NiggleAmp.Services;
using gbL.NetBoard;

namespace NiggleAmp.Web.Services
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class TypesHandler : ITypesHandler
    {
        private IWire Wire { get; set; }

        public TypesHandler()
        {
            Wire = HubService.BuildWire();
        }

        public NAmpType[] ListFor(string instanceId)
        {
            return BuildTypes(GetLinkBuilder(instanceId));
        }

        public NAmpTypeDetail DetailOf(string instanceId, string typeId)
        {
            var matchedType = BuildTypes(GetLinkBuilder(instanceId)).FirstOrDefault(x => x.Value.Equals(typeId));

            var ret = new NAmpTypeDetail
                {
                    Type = matchedType,
                    Items = new[] { new NAmpItem("Epic"), new NAmpItem("Story"), new NAmpItem("Task"), }
                };

            return ret;
        }

        private static Func<string, NAmpLink[]> GetLinkBuilder(string instanceId)
        {
            return objectId => new[]
                {
                    new NAmpLink() { Uri = string.Format("/types/{0}/{1}", instanceId, objectId), Rel = "detail" }
                };
        }

        private NAmpType[] BuildTypes(Func<string, NAmpLink[]> linkBuilder)
        {
            return new[]
                {
                    new NAmpType()
                        {
                            Name = "Object",
                            PluralName = "Objects",
                            Value = "1",
                            Links = linkBuilder("1")
                        },
                    new NAmpType()
                        {
                            Name = "Property", 
                            PluralName = "Properties",
                            Value = "2",
                            Links = linkBuilder("2")
                        },
                };
        }

    }
}
